home *** CD-ROM | disk | FTP | other *** search
Text File | 1990-12-05 | 7.0 KB | 254 lines | [TEXT/AOqc] |
- {}
- { PICS Unit - Steve Sheets}
- {}
- { This Unit provides the Interface to the PICS data structure}
- { as well as the procedures to create, dispose and draw the}
- { PICS information.}
-
- unit PICSUnit;
-
- interface
-
- const
- kPICStype = 'PICS'; {File type of PICS}
- kINFOtype = 'INFO'; {Resource type of PICS information resource}
-
- { Interface for the optional information handle and the PICS handle}
- { which contains 1 or more Pictures and the information handle.}
- { Notice that the PICS handle is a variable length handle based on}
- { the number of frames.}
-
- type
- TPICSInfoRec = record
- BWColor: INTEGER; {0 = Black & White, 1 = Color}
- Depth: INTEGER; {1,2,4,8,16 pixel depth}
- Speed: INTEGER; {1..200 frames per sec, else negative seconds per frame}
- Version: INTEGER; {0 currently}
- Creator: ResType; {original creator signature}
- Largest: LongInt; {if non-zero, largest picture size}
- end;
- TPICSInfoPtr = ^TPICSInfoRec;
- TPICSInfoHdl = ^TPICSInfoPtr;
-
- TPICSRec = record
- NumFrames, DimH, DimV: INTEGER;
- PICSInfoHdl: TPICSInfoHdl;
- Frame: array[1..1] of PicHandle;
- end;
- TPICSPtr = ^TPICSRec;
- TPICSHdl = ^TPICSPtr;
-
-
- { Given a file name and volume reference number, try to read the PICS}
- { file at that location. If successful, return noErr in the function and }
- { the information in thePICS parameter. If there was a problem, return}
- { the error number in the function, and thePICS is set to NIL.}
-
- function ReadPICS (theFileName: Str255;
- theVRefNum: INTEGER;
- var thePICS: TPICSHdl): OSErr;
-
- { Given thePICS data, dispos of all the handles and data structures.}
-
- procedure DisposePICS (thePICS: TPICSHdl);
-
- { Given thePICS data, and an V & H position to draw at, draw the}
- { animation. The Loopflag tells the procedure to either loop the}
- { animation continously (TRUE), or only draw once time (FALSE).}
- { The ScanKeyFlag tells the procedure if it should look to see if}
- { someone has pressed a key during animation. If so, the procedure}
- { is stopped at that point. Notice that it is dangerous to have }
- { Loopflag set TRUE and ScanKeyFlag set FALSE (infinite loop time).}
-
- procedure DrawPICS (thePICS: TPICSHdl;
- HPos, VPos: INTEGER;
- LoopFlag, ScanKeyFlag: BOOLEAN);
-
- implementation
-
- { Simple utility function, given number of frames, size of the TPICSRec}
- { record in bytes.}
-
- function PICSsize (theNumFrames: INTEGER): INTEGER;
- begin
- PICSsize := (theNumFrames * 4) + 10;
- end;
-
- function ReadPICS (theFileName: Str255;
- theVRefNum: INTEGER;
- var thePICS: TPICSHdl): OSErr;
- var
- tempE: OSErr;
- tempResNum: INTEGER;
- tempSize: INTEGER;
- tempPICS: TPICSHdl;
- tempPicture: PicHandle;
- tempFlag: BOOLEAN;
- begin
- thePICS := nil;
- tempPICS := nil;
- tempPicture := nil;
-
- tempResNum := OpenRFPerm(theFileName, theVRefNum, 0);
- if tempResNum = -1 then
- tempE := ResError
- else
- begin
- tempPicture := PicHandle(Get1Resource('PICT', 128));
- if tempPicture = nil then
- tempE := ResError
- else
- begin
- HNoPurge(Handle(tempPicture));
- DetachResource(Handle(tempPicture));
-
- tempSize := 100;
- tempPICS := TPICSHdl(NewHandle(PICSsize(tempSize)));
- if tempPICS = nil then
- tempE := MemError
- else
- begin
- with tempPICS^^ do
- begin
- NumFrames := 1;
- with tempPicture^^.picFrame do
- begin
- DimH := Right - Left;
- DimV := Bottom - Top;
- end;
- PICSInfoHdl := nil;
- Frame[1] := tempPicture;
- end;
- tempPicture := nil;
-
- tempPICS^^.PICSInfoHdl := TPICSInfoHdl(Get1Resource(kINFOtype, 128));
- if tempPICS^^.PICSInfoHdl <> nil then
- begin
- HNoPurge(Handle(tempPICS^^.PICSInfoHdl));
- DetachResource(Handle(tempPICS^^.PICSInfoHdl));
- end;
-
- tempFlag := FALSE;
- repeat
- tempPicture := PicHandle(Get1Resource('PICT', 128 + tempPICS^^.NumFrames));
- if tempPicture = nil then
- begin
- tempE := ResError;
- if (tempE = resNotFound) or (tempE = noErr) then
- begin
- tempE := noErr;
- SetHandleSize(Handle(tempPICS), PICSsize(tempPICS^^.NumFrames));
- thePICS := tempPICS;
- tempPICS := nil;
- end;
- tempFlag := TRUE;
- end
- else
- begin
- HNoPurge(Handle(tempPicture));
- DetachResource(Handle(tempPicture));
- if tempPICS^^.NumFrames = tempSize then
- begin
- tempSize := tempSize + 100;
- SetHandleSize(Handle(tempPICS), PICSsize(tempSize));
- tempE := ResError;
- end;
- if tempE = noErr then
- begin
- tempPICS^^.NumFrames := tempPICS^^.NumFrames + 1;
- tempPICS^^.Frame[tempPICS^^.NumFrames] := tempPicture;
- tempPicture := nil;
- end
- else
- tempFlag := TRUE;
- end;
- until tempFlag;
- end;
-
- end;
- CloseResFile(tempResNum);
- end;
-
- if tempPICS <> nil then
- DisposePICS(tempPICS);
- if tempPicture <> nil then
- DisposHandle(Handle(tempPicture));
-
- ReadPICS := tempE;
- end;
-
- procedure DisposePICS (thePICS: TPICSHdl);
- var
- tempNum: INTEGER;
- begin
- if thePICS <> nil then
- begin
- if thePICS^^.PICSInfoHdl <> nil then
- DisposHandle(Handle(thePICS^^.PICSInfoHdl));
- for tempNum := 1 to thePICS^^.NumFrames do
- DisposHandle(Handle(thePICS^^.Frame[tempNum]));
- DisposHandle(Handle(thePICS));
- end;
- end;
-
- procedure DrawPICS (thePICS: TPICSHdl;
- HPos, VPos: INTEGER;
- LoopFlag, ScanKeyFlag: BOOLEAN);
- var
- tempRect: Rect;
- tempTicks: LongInt;
- tempDone: BOOLEAN;
- tempCount: INTEGER;
-
- { Wait tempTicks number of ticks, stopping at any time if mouse or key is pressed.}
-
- procedure WaitFrame;
- var
- tempLong: LongInt;
- tempEvent: EventRecord;
- begin
- tempLong := tickCount + tempTicks;
- while (tempLong > tickCount) and (not tempDone) do
- begin
- SystemTask;
- tempDone := GetNextEvent(mDownMask + keyDownMask + autoKeyMask, tempEvent);
- end;
- end;
-
- begin
- if thePICS <> nil then
- if thePICS^^.NumFrames > 0 then
- if thePICS^^.Frame[1] <> nil then
- begin
- tempDone := FALSE;
- if thePICS^^.PICSInfoHdl <> nil then
- begin
- tempTicks := thePICS^^.PICSInfoHdl^^.Speed;
- if tempTicks <= 0 then
- tempTicks := -60 * tempTicks
- else
- tempTicks := 60 div tempTicks;
- end
- else
- tempTicks := 6;
-
- repeat
- tempCount := 0;
- repeat
- tempCount := tempCount + 1;
-
- if thePICS^^.Frame[tempCount] <> nil then
- if not EmptyRect(thePICS^^.Frame[tempCount]^^.PicFrame) then
- begin
- tempRect := thePICS^^.Frame[tempCount]^^.PicFrame;
- OffSetRect(tempRect, HPos, VPos);
- DrawPicture(thePICS^^.Frame[tempCount], tempRect);
- end;
- WaitFrame;
-
- until tempDone or (tempCount >= thePICS^^.NumFrames);
- until tempDone or (not LoopFlag);
- end;
- end;
-
- end.